package com.minelx.sqlstructures.statements;

import com.minelx.sqlstructures.statements.clause.*;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Query {
	private final List<Field> fields;

	private final AndBuilder where;

	private Sql orderedBy;

	private Sql groupBy;

	private Sql limitation;

	private Query() {
		fields = new LinkedList<>();
		where = AndBuilder.empty();
		orderedBy = Sql.empty();
		groupBy = Sql.empty();
		limitation = Sql.empty();
	}

	public AndBuilder where() {
		return where;
	}

	public Query field(Field added) {
		fields.add(added);
		return this;
	}

	public String statementOf(String table) {
		SelectBuilder result = SelectBuilder.create(table);
		result.select().addAll(fields);
		result.where().addAll(where.conditions());
		result.groupBy(groupBy);
		result.orderBy(orderedBy);
		result.limit(limitation);
		return result.build().toText();
	}

	public Query orderedBy(Sql orderedBy) {
		this.orderedBy = orderedBy;
		return this;
	}

	public Query groupBy(Sql groupBy) {
		this.groupBy = groupBy;
		return this;
	}

	public Query limit(Sql limitation) {
		this.limitation = limitation;
		return this;
	}

	public static Query create() {
		return new Query();
	}
}
